%{

#include "main.h"
#include "lex.h"

struct token_struct {
    string text ;
    int type ;
} ;

token_struct token ;
string kls[]={
    "and"          , "always"      , "assign"     , "begin"     , "buf"         ,
    "bufif0"       , "bufif1"      , "case"       , "casex"     , "casez"       ,
    "cmos"         , "deassign"    , "default"    , "defparam"  , "disable"     ,
    "edge"         , "else"        , "end"        , "endcase"   , "endfunction" ,
    "endprimitive" , "endmodule"   , "endspecify" , "endtable"  , "endtask"     ,
    "event"        , "for"         , "force"      , "forever"   , "fork"        ,
    "function"     , "highz0"      , "highz1"     , "if"        , "ifnone"      ,
    "initial"      , "inout"       , "input"      , "integer"   , "join"        ,
    "large"        , "macromodule" , "medium"     , "module"    , "nand"        ,
    "negedge"      , "nor"         , "not"        , "notif0"    , "notif1"      ,
    "nmos"         , "or"          , "output"     , "parameter" , "pmos"        ,
    "posedge"      , "primitive"   , "pulldown"   , "pullup"    , "pull0"       ,
    "pull1"        , "rcmos"       , "real"       , "realtime"  , "reg"         ,
    "release"      , "repeat"      , "rnmos"      , "rpmos"     , "rtran"       ,
    "rtranif0"     , "rtranif1"    , "scalared"   , "small"     , "specify"     ,
    "specparam"    , "strength"    , "strong0"    , "strong1"   , "supply0"     ,
    "supply1"      , "table"       , "task"       , "tran"      , "tranif0"     ,
    "tranif1"      , "time"        , "tri"        , "triand"    , "trior"       ,
    "trireg"       , "tri0"        , "tri1"       , "vectored"  , "wait"        ,
    "wand"         , "weak0"       , "weak1"      , "while"     , "wire"        ,
    "wor"          , "xnor"        , "xor"        , "logic"    
};

int id_or_kwd(string wd){
    int n=1 ;
    for(string kwd:kls){
        if(wd.compare(kwd)==0){
            return n ;
        }
        n++ ;
    }
    return 0 ;
}
%}

%x SCOMMENT MCOMMENT

digit [0-9]
alpha [a-zA-Z]
underline "_"



dec {digit}+
hex ({digit}+)"'h"([0-9a-f])+
bin ({digit}+)"'b"([0-1])+
oct ({digit}+)"'o"([0-7])+

identifier ({underline}|{alpha})({digit}|{alpha}|{underline})*
macro "`"{identifier}
string "\""(.*)"\""

%%

"#" {
    return HASH ;
}

"=" {
    return EQUAL ;
}

"\(" {
    return LPAREN ;
}
"\)" {
    return RPAREN ;
}
"[" {
    return LBRACK  ;
}
"]" {
    return RBRACK  ;
}
"{" {
    return LBRACE  ;
}
"}" {
    return RBRACE  ;
}

";" {
    return SEM ;
}
"," {
    return COMMA ;
}
"\x2e" {
    return DOT ;
}

"+" {
    return ADD ;
}
"-" {
    return SUB ;
}
"*" {
    return MUL ;
}
"**" {
    return POW ;
}
"/" {
    return DIV ;
}

"@" {
    return AT;
}
":" {
    return COLON ;
}
{macro} {
    string s=yytext ;
    if(s.compare("`ifdef")==0){
        return IFDEF ;
    }
    if(s.compare("`ifndef")==0){
        return IFNDEF ;
    }
    if(s.compare("`else")==0){
        return ELSE ;
    }
    if(s.compare("`endif")==0){
        return ENDIF ;
    }
    if(s.compare("`include")==0){
        return INCLUDE ;
    }

    return MACRO ;
        }
{dec} {
            return DEC ;
          }

{bin} {
            return BIN ;
          }
{hex} {
          return HEX ;
      }
{oct} {
          return OCT ;
      }
{identifier} {
                if(id_or_kwd(yytext)){
                    return id_or_kwd(yytext) ;
                }
                else{
                    return ID ;
                }
             }
"//" {
    BEGIN SCOMMENT ;
}
<SCOMMENT>\n {
    BEGIN INITIAL ;
}
<SCOMMENT>. {}

"/\*" {
    BEGIN MCOMMENT ;
}

<MCOMMENT>"\*/" {
    BEGIN INITIAL ;
}
<MCOMMENT>(.|\n) {}

{string} {
    return STRING ;
}


. 
[ \t\n]
%%

void setfile(char file[]){

    FILE* f ;

    f = fopen(file,"r") ;
    yyin = f ;
}

char* get_string(){
    return yytext ;
}


int yywrap(void){
    return 1 ;
}
void version(){
    cout << "jsz_ver:Sun Jul  7 20:46:17 2024" << endl  ;
}
/*int main(){*/
    /*FILE* f ;*/
    /*f = fopen("t.txt","r") ;*/
    /*yyin = f ;*/
    /*int tok ;*/
    /*while(tok=yylex()){*/
        /*cout << tok << endl ;*/
    /*}*/
    /*fclose(f) ;*/
    /*cout << id_or_kwd("jdz") << endl  ;*/
    /*return 0 ;*/
/*}*/
